package com.xiaolanshu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaolanshu.entity.Note;
import com.xiaolanshu.entity.User;
import com.xiaolanshu.interceptor.UserHolder;
import com.xiaolanshu.mapper.NoteMapper;
import com.xiaolanshu.mapper.UserMapper;
import com.xiaolanshu.service.NoteService;
import com.xiaolanshu.vo.NoteVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class NoteServiceImpl  implements NoteService {
    @Autowired
    private NoteMapper noteMapper;
    @Autowired
    private UserMapper userMapper;

    //根据整型数组查询笔记
    @Override
    public List<Note> noteArrList(int[] arr) {
        //遍历数组将查询到的笔记保存到新建的笔记集合
        List<Note> notes = new ArrayList<>();
        for (int i:arr
             ) {
            //构造查询条件
            LambdaQueryWrapper<Note> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Note::getId,i).eq(Note::getDeleted,0);
            Note note = noteMapper.selectOne(lqw);

            notes.add(note);
        }

        return notes;
    }

    //获取所有笔记信息
    @Override
    public List<Note> noteList() {

        //构建条件，查询的类型为Note对象
        QueryWrapper<Note> qw = new QueryWrapper<>();
        return noteMapper.selectList(qw);
    }


    //获取笔记内容
    @Override
    public Note findNoteById(Long noteId) {
        //构造查询条件
        QueryWrapper<Note> qw = new QueryWrapper<>();
        qw.eq("id",noteId).eq("deleted",0);
        return noteMapper.selectOne(qw);
    }
    //根据笔记标题、内容、标签模糊查询
    @Override
    public List<NoteVo> searchList(String param) {
        //构造条件，根据标签、视频介绍、标题模糊查询
        LambdaQueryWrapper<Note> lqw = new LambdaQueryWrapper<>();
        lqw.like(Note::getContent,param).or().like(Note::getTag,param).or().like(Note::getTitle,param).eq(Note::getDeleted,0);
        List<Note> notes = noteMapper.selectList(lqw);

        //遍历notes，获取noteId，根据noteId查询用户信息
        List<NoteVo>  noteVoList = new ArrayList<>();
        for (Note n:notes
             ) {
            LambdaQueryWrapper<User> lqw2 = new LambdaQueryWrapper<>();
            lqw2.eq(User::getId,n.getUserId());
            User user = userMapper.selectOne(lqw2);
            //创建一个noteVo对象保存笔记信息和用户信息
            NoteVo noteVo = new NoteVo();
            noteVo.setNote(n);
            noteVo.setUser(user);
            //保存遍历得到的noteVo集合
            noteVoList.add(noteVo);
        }
        return noteVoList;
    }
    //保存笔记信息
    @Override
    public Long saveNote( Map param) {
        //从线程当中获取用户id和用户名（拦截器从请求头获取了用户信息并缓存到线程当中）
        Long userId = UserHolder.getUserId();
        String username = UserHolder.getUsername();

        //获取请求参数中的笔记信息并保存
        Note note = new Note();
        note.setUserId(userId);
        note.setUsername(username);
        note.setTitle((String) param.get("title"));
        note.setContent((String) param.get("content"));
        note.setSurfacePicture((String) param.get("surfacePicture"));
        note.setTag((String) param.get("tag"));
        noteMapper.insert(note);
        //保存完返回主键id
        return note.getId();
    }
    //根据用户id分页获取笔记信息
    @Override
    public Page<Note> notePage(int page, int pageSize, Long userId) {
        //构造分页构造器
        Page<Note> pageInfo = new Page<>(page,pageSize);

        //添加过滤条件
        LambdaQueryWrapper<Note> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Note::getUserId,userId).eq(Note::getDeleted,0);
        return noteMapper.selectPage(pageInfo,lqw);
    }
    //根据用户id和笔记id删除笔记
    @Override
    public int removeNote(Long userId, Long noteId) {
        //根据笔记id和用户id删除笔记
        Note note = new Note();
        note.setId(noteId);
        note.setUserId(userId);
        //更改表中的deleted为1,默认0,1为删除
        note.setDeleted(1);
        //构造条件
        LambdaQueryWrapper<Note> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Note::getUserId,userId).eq(Note::getId,noteId);

        return noteMapper.update(note, lqw);
    }
}
